package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.asn1.axdr.AxdrOptional;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

public class ActionThenGetResponseNormalListItem implements AxdrType {

    private byte[] dataCode = null;
    private OMD omd = null;
    private AxdrEnum dar = null;
    private AxdrOptional<Data> retData = new AxdrOptional<Data>(new Data(), false);
    private ActionThenGetResult result = null;

    public ActionThenGetResponseNormalListItem() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OMD omd, AxdrEnum dar, Data retData, ActionThenGetResult result) {
        this.omd = omd;
        this.dar = dar;
        this.retData.setValue(retData);
        this.result = result;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        try {
            int codeLength = 0;

            this.omd = new OMD();
            codeLength += this.omd.decode(input);

            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);

            this.retData = new AxdrOptional<Data>(new Data(), false);
            codeLength += this.retData.decode(input);

            this.result = new ActionThenGetResult();
            codeLength += this.result.decode(input);

            return codeLength;
        } catch (IOException e) {
            throw new RuntimeException("Error decoding ActionThenGetResponseNormalListItem", e);
        }
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        try {
            if (this.dataCode != null) {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
                return this.dataCode.length;
            } else {
                if (this.dar != null && this.omd != null && this.retData != null && this.result != null) {
                    int codeLength = 0;
                    codeLength += this.result.encode(output);
                    codeLength += this.retData.encode(output);
                    codeLength += this.dar.encode(output);
                    codeLength += this.omd.encode(output);
                    return codeLength;
                } else {
                    throw new RuntimeException("retData || dar || omd || result is null");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Error encoding ActionThenGetResponseNormalListItem", e);
        }
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding ActionThenGetResponseNormalListItem", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.omd != null && this.dar != null && this.retData != null && this.result != null) {
            return "sequence: {" + "omd: " + this.omd + ", dar: " + this.dar + ", retData: " + this.retData
                    + ", result: " + this.result + "}";
        } else {
            return "retData || dar || omd || result is null";
        }
    }

    public OMD getOmd() {
        return omd;
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public AxdrOptional<Data> getRetData() {
        return retData;
    }

    public ActionThenGetResult getResult() {
        return result;
    }
}